<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>引出生命周期</title>
</head>
<body>
  <!-- 准备容器 -->
  <div id="test"></div>
  <!-- 按照顺序引入 -->
  <script type="text/javascript" src="../../js/react.development.js"></script>
  <script type="text/javascript" src="../../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../../js/babel.min.js"></script>
  <script type="text/babel" >
    /*
        生命周期
        1.挂载(mount)
        2.卸载(unmount)
    */



    class Life extends React.Component {
        state = {opacity:0.2}
        death=()=>{
            // 清除定时器
            // clearInterval(this.timer)
            // 卸载组件  unmountComponentAtNode(卸载的节点)
            ReactDOM.unmountComponentAtNode(document.getElementById('test'))
        }

        // 组件挂载完毕之后
        componentDidMount(){
            this.timer = setInterval(() => {
                let {opacity} = this.state
                opacity-=0.1
                if(opacity<=0) opacity =1
                this.setState({opacity})
            }, 200);
        }
        // 组件将要被卸载
        componentWillUnmount(){
            // 清除定时器
            clearInterval(this.timer)
        }
        // render调用时机   初始化渲染   状态更新之后
        render() {
            return (
                <div>
                    <h1 style={{opacity:this.state.opacity}}>React 学不会怎么办</h1>
                    <button onClick={this.death}>不活了</button>
                </div>
            )
        }
    }
    // 挂载组件
    ReactDOM.render(<Life/>,document.getElementById('test'))
  </script>
</body>
</html>